package cn.itrip.biz.service.impl;


import cn.itrip.beans.pojo.ItripHotel;
import cn.itrip.beans.pojo.ItripLabelDic;
import cn.itrip.beans.vo.ImagesVo;
import cn.itrip.beans.vo.biz.*;
import cn.itrip.biz.service.IItripCommentService;
import cn.itrip.common.Page;
import cn.itrip.dao.ItripCommentMapper;
import cn.itrip.dao.ItripHotelRoomMapper;
import cn.itrip.dao.ItripImageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 滕召伟
 * @since 2020-02-27
 */
@Service
public class ItripCommentServiceImpl implements IItripCommentService {
    @Resource
    ItripCommentMapper itripCommentMapper;
    @Resource
    ItripHotelRoomMapper itripHotelRoomMapper;
    @Resource
    ItripImageMapper itripImageMapper;

    /**
     * 获取酒店等级
     *
     * @param hotelId
     * @return
     */
    @Override
    public ItripHotel getHotelId(Long hotelId) {
        try {
            if (hotelId != null && !"".equals(hotelId)) {
                ItripHotel comment = itripCommentMapper.getHotelId(hotelId);
                return comment;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;

    }

    /**
     * 出游类型
     *
     * @return
     */
    @Override
    public List<ItripLabelDic> listParentId() {
        try {
            List<ItripLabelDic> labelDicList = itripCommentMapper.listParentId();
            if (labelDicList.size() > 0) {
                return labelDicList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 酒店点评
     *
     * @param imagesVo
     */
    @Override
    public void addComment(ImagesVo imagesVo) {
        try {
            if (imagesVo != null) {
                itripCommentMapper.addComment(imagesVo);
                itripCommentMapper.updateOrderId(imagesVo.getOrderId());
                for (int i = 0; i < imagesVo.getItripImages().size(); i++) {
                    String name = imagesVo.getItripImages().get(i).toString();
                    String name1 = "{imgUrl=";
                    String name2 = "}";
                    String Url = name.replace(name1, "");
                    String Url1 = Url.replace(name2, "");
                    imagesVo.setImagesURL("http://img.itrip.test.com/image/" + Url1);

                    imagesVo.setPositionOrder(i + 1);
                    itripCommentMapper.saveImages(imagesVo);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public GetHotelScore GetHotelScore(Integer hotelId) {
        return itripCommentMapper.GetHotelScore(hotelId);
    }

    @Override
    public GetCount GetCount(Integer hotelId) {
        return itripCommentMapper.GetCount(hotelId);
    }

    @Override
    public Page<GetCommentListVo> GetCommentList(GetCommentList list) {
        Page<GetCommentListVo> pageUtil = new Page<>();
        if (list.getPageNo() == null || list.getPageNo() == 0) {
            list.setPageNo(1);
        } else if (list.getPageSize() == null || list.getPageSize() == 0) {
            list.setPageSize(5);
        }
        List<GetCommentListVo> listVos = itripCommentMapper.GetCommentList(list);
        for (GetCommentListVo vo : listVos) {
            List<String> stringList = itripHotelRoomMapper.GetCommentListByHotelId(list.getHotelId());
            Integer count = Integer.parseInt(vo.getRoomTitle());
            if (count > 0) {
                count = count - 1;
            }
            vo.setRoomTitle(stringList.get(count));
        }
        pageUtil.pageUtil(list.getPageNo(), list.getPageSize(), listVos);
        return pageUtil;
    }

    @Override
    public List<GetImg> GetImg(Integer id) {
        return itripImageMapper.GetImg(id);
    }
}